--UI窗体管理器
module("UI.UIWinMgr",package.seeall)

UIWinMgr = {}

UIWinMgr.WinSet = {}

UIWinMgr.aniWin = {}

-- function UIWinMgr:GetLastTB(tb)
--     local key,value
--     if tb then
--         for i,_value in pairs(tb) do
--             key=i
--             value=_value
--              print(value)
--         end
--     end
--     return key,value
-- end


function UIWinMgr:CheckTB(tb,data)
    if tb then
        for i,_value in pairs(tb) do
            if data==_value then
                return 1
            end
        end
    end
    return 0
end


function UIWinMgr:CloseUpWin()

   local key=UIWinMgr.aniWin[#UIWinMgr.aniWin]
    if key~= nil then
       local window = UIWinMgr:GetWindow(key)
        if nil ~= window then
            window:Close(1)
        end
        if key=="ZJHRoomWin" or key=="ZJHMatchRoomWin" or key=="BYQNNRoomWin" or key=="BYQNNMatchRoomWin" then
            UIWinMgr:CloseUpWin()
        else
            return 1
        end
    else
        return 0
    end
   
end


--获得界面
function UIWinMgr:GetWindow(WindowName,openAniType)
    window = UIWinMgr.WinSet[WindowName]
    if WindowName=="ZJHFiveGameWin" then
        UIWinMgr.aniWin={}
    end
 
    if openAniType ==1 then
        --
      
       if UIWinMgr:CheckTB( UIWinMgr.aniWin,WindowName)==0 then
        UIWinMgr.aniWin[#UIWinMgr.aniWin+1]=WindowName;
        local newGameLoobyWin = UIWinMgr.WinSet["NewGameLoobyWin"]
       if newGameLoobyWin~=nil then
            newGameLoobyWin.Container.gameObject:SendMessage("SetLuaData",#UIWinMgr.aniWin)
       end
	
       
      -- print("界面数量"..#UIWinMgr.aniWin)
       end
    end
    if nil ~= window then
        if nil == window.Root or window.Root:Equals(nil) or nil == window.Container or window.Container:Equals(nil) then
            window:CreateWin(openAniType)
            window:Init()
            if nil ~= window.Container then
                window.Container.gameObject:SetActive(false) --黄老板要求界面第一次搞出来的时候要是关闭状态
            end
        end
    else
        Debug.log("获取界面 "..WindowName.." 失败")
    end
   
    return window
end

--打开界面
function UIWinMgr:OpenWindow(WindowName,openAniType)
    window = UIWinMgr:GetWindow(WindowName,openAniType)
    if nil ~= window then
        window:Show(openAniType)
    end
    return window
end

--关闭界面
function UIWinMgr:CloseWindow(WindowName)
    window = UIWinMgr:GetWindow(WindowName)
    if nil ~= window then
        window:Close()
    end
    return window
end

---打开文字提示界面
function UIWinMgr:OpenTextNotice(msg)
    local noticeWin = UIWinMgr:OpenWindow("NewGameTextNoticeWin",2)
    noticeWin:SetMsgInfo(msg)
    return noticeWin
end



--打开提示界面
function UIWinMgr:OpenNotice(msg,okfun,nofun)
    -- local noticeWin = UIWinMgr:OpenWindow("NoticeWin")
    local noticeWin = UIWinMgr:OpenWindow("NewGameNoticeWin",2)
    --noticeWin.Container.depth = 99999
    noticeWin:SetMsg(msg,okfun,nofun)
    noticeWin:BringToTop()
    return noticeWin
end

--打开新版提示界面
function UIWinMgr:OpenNewGameNotice(msg,okfun,nofun)
    local NewGameNoticeWin = UIWinMgr:OpenWindow("NewGameNoticeWin",2)
    --noticeWin.Container.depth = 99999
    if msg then
        NewGameNoticeWin:SetMsg(msg,okfun,nofun)
        NewGameNoticeWin:BringToTop()
    end
    return NewGameNoticeWin
end

function UIWinMgr:OpenErrorNotice(code)
    local DTMgr = require("DataTable").DTMgr
    local NoticeText = DTMgr:GetTabel("NoticeText")
    local str = DTMgr:GetValueByKey(NoticeText,"value",code,"text")
    if not str then
        Debug.log("服务器Notice："..code.." 没有定义")
        return
    end

    --TODO 暂时去掉这个“不在此状态中”的提示
    if code == 131 then
        return
    end
    --self:OpenNotice(str)
    self:OpenNewGameNotice(str)
end


-----获取翻译文字
function UIWinMgr:GetIntTextByCode(code)
    local DTMgr = require("DataTable").DTMgr
    local InternationalCommonNotice = DTMgr:GetTabel("InternationalCommonNotice")
    local str = DTMgr:GetValueByKey(InternationalCommonNotice,"value",code,"text")
    if not str then
        
        return
    end
    return str
end

function UIWinMgr:OpenMask(msg)
    local MaskWin = UIWinMgr:OpenWindow("MaskWin",2)
    -- Debug.dump(MaskWin, "MaskWin")
    Debug.log("-----------OpenMask---------------open")
    MaskWin:SetTxt(msg)
    MaskWin:BringToTop()
    return MaskWin
end

function UIWinMgr:CloseMask()
    UIWinMgr:CloseWindow("MaskWin")
    Debug.log("-----------CloseMask---------------open")
end
